﻿using System;
using System.IO;
using System.Reflection;
using CuteSprite.Eme.Data;
using CuteSprite.Eme.UI.Utils;
using HmFramework.Components.Controls;
using HmFramework.Data;
using HmFramework.Data.Common;
using HmFramework.UI.Utils;
using HmFramework.UI.Utils.Controls;

namespace CuteSprite.Eme.UI.BsdUserCenter
{
	public partial class FrmBsdOrganize : EmeEntityForm
	{
		#region -- 构造 --

		#region - 新增 -

		/// <summary>默认构造函数</summary>
		public FrmBsdOrganize()
			: base()
		{
			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="templateFileName">模板文件</param>
		public FrmBsdOrganize(String templateFileName)
		{
			SetupFromXml(templateFileName);
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		public FrmBsdOrganize(String resourceName, Assembly asm)
		{
			SetupFromXmlResource(resourceName, asm);
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="stream">模板数据流</param>
		public FrmBsdOrganize(Stream stream)
		{
			SetupFromXmlStream(stream);
			InitForm();
		}

		#endregion

		#region - 编辑或新增 -

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		public FrmBsdOrganize(IEntity entity)
			: this(entity, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBsdOrganize(IEntity entity, Boolean readOnly)
			: base(entity, readOnly)
		{
			InitializeComponent();
			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="templateFileName">模板文件</param>
		public FrmBsdOrganize(IEntity entity, String templateFileName)
			: this(entity, templateFileName, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="templateFileName">模板文件</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBsdOrganize(IEntity entity, String templateFileName, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXml(templateFileName);

			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		public FrmBsdOrganize(IEntity entity, String resourceName, Assembly asm)
			: this(entity, resourceName, asm, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="resourceName">模板资源文件</param>
		/// <param name="asm">模板资源文件所在程序集</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBsdOrganize(IEntity entity, String resourceName, Assembly asm, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXmlResource(resourceName, asm);

			InitForm();
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="stream">模板数据流</param>
		public FrmBsdOrganize(IEntity entity, Stream stream)
			: this(entity, stream, false)
		{
		}

		/// <summary>构造函数</summary>
		/// <param name="entity">实体对象</param>
		/// <param name="stream">模板数据流</param>
		/// <param name="readOnly">是否只读，指定编辑还是浏览实体信息</param>
		public FrmBsdOrganize(IEntity entity, Stream stream, Boolean readOnly)
			: base(entity, readOnly)
		{
			SetupFromXmlStream(stream);

			InitForm();
		}

		#endregion

		#endregion

		#region -- 窗体事件 --

		#region - method InitComponent -

		protected override void InitComponent()
		{
			base.InitComponent();

			if (frmParentID == null) { frmParentID = FindControl("frmOrganizeID") as EntitySelector; }
			if (frmCategory == null) { frmCategory = FindControl("frmCategory") as ComboBoxEx; }

			// 窗体事件，确保动态模板调用
			this.OnValid += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdOrganize_OnValid);
			this.OnSaving += new System.EventHandler<CuteSprite.Eme.UI.Utils.EntityFormEventArgs>(this.FrmBsdOrganize_OnSaving);
		}

		#endregion

		#region - method FormOnPreLoad -

		protected override void FormOnPreLoad()
		{
			base.FormOnPreLoad();
			if (IsAddNew)
			{
				CurrentEntity[Organize.__.IsInnerOrganize] = true;
			}
		}

		#endregion

		#endregion

		private void FrmBsdOrganize_OnValid(object sender, EntityFormEventArgs e)
		{
			if (IsAddNew)
			{
				if (frmParentID != null && frmParentID.SelectedID > 0 && frmCategory != null && (Int32)frmCategory.SelectedValue == (Int32)OrganizeCategoryFlag.Company)
				{
					DialogHelper.ShowWarning(null, "公司类型的组织机构不允许存在上级机构！");
					e.Cancel = true;
				}
			}
			else
			{
				if (!Helper.IsNullKey(CurrentEntity[Organize.__.ParentID]) && frmCategory != null && (Int32)frmCategory.SelectedValue == (Int32)OrganizeCategoryFlag.Company)
				{
					DialogHelper.ShowWarning(null, "公司类型的组织机构不允许存在上级机构！");
					e.Cancel = true;
				}
			}
		}

		private void FrmBsdOrganize_OnSaving(object sender, EntityFormEventArgs e)
		{
			if (frmParentID != null && frmParentID.SelectedID < 1)
			{
				CurrentEntity[Organize.__.Category] = (Int32)OrganizeCategoryFlag.Company;
			}
		}
	}
}
